package com.originskyseed.platform.user.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerRelateConsumeMapper;
import com.originskyseed.platform.user.model.PlayerRelateConsume;
import com.originskyseed.platform.user.service.IPlayerRelateConsumeService;

/**
 * 玩家消费信息表Service接口实现类（OSI_PLAYER_RELATE_CONSUME）
 *
 * @author wlj
 */
@DubboService
@Component("playerRelateConsumeService")
public class PlayerRelateConsumeServiceImpl implements IPlayerRelateConsumeService {

	@Resource
	private PlayerRelateConsumeMapper playerRelateConsumeMapper;


	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateConsumeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateConsumeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateConsume record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateConsumeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateConsume record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateConsumeMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateConsume record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateConsumeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateConsume record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateConsumeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询玩家对主播的消费记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public BigDecimal countAmountByIdAndAnchorId(String id, String anchorId) throws Exception {
		return this.playerRelateConsumeMapper.countAmountByIdAndAnchorId(id, anchorId);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateConsume selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateConsumeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateConsume> selectList(PlayerRelateConsume record) throws Exception {
		return this.playerRelateConsumeMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateConsume> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateConsume> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateConsume> list = this.playerRelateConsumeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<PlayerRelateConsume> list) throws Exception {
		return this.playerRelateConsumeMapper.insertBatch(list);
	}


	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<PlayerRelateConsume> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertBatch(list);
	}

	/**
	 * 批量插入记录,月表
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(List<PlayerRelateConsume> list) throws Exception{
		return this.playerRelateConsumeMapper.insertMonthBatch(list);
	}


	/**
	 * 批量插入记录,月表
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(String tenantCode,List<PlayerRelateConsume> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertMonthBatch(list);
	}

	/**
	 * 分页查询消费明细列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateConsume> selectConsumptionDetailsByPage(Map<String, Object> params, BasePage<PlayerRelateConsume> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateConsume> list = this.playerRelateConsumeMapper.selectConsumptionDetailsByPage(params);
		if(list!=null&&list.size()>0) {
			for(int i=0;i<list.size();i++) {
				PlayerRelateConsume playerRelateConsume=list.get(i);
				String channelSubId = playerRelateConsume.getChannelSubId();
				if (channelSubId != null && StringUtils.isNotBlank(channelSubId)) {
					UserInfo userInfo = userInfoMapper.selectByOtherAccount(channelSubId);
					playerRelateConsume.setBelongBusinessName(userInfo == null ? "" : userInfo.getName());
				}
			}
		}
		page.setResults(list);
		return page;
	}

	/**
	 * 查询消费明细列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateConsume> selectConsumptionDetailsByMap(Map<String, Object> params) throws Exception{
		return this.playerRelateConsumeMapper.selectConsumptionDetailsByMap(params);
	}

	/**
	 * 查询orderId是否存在
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectOrderIds(String tenantCode,List<String> list) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		//System.out.println("==selectOrderIds="+tenantCode);
		return this.playerRelateConsumeMapper.selectOrderIds(list);
	}


	/**
	 * 查询orderId是否存在
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectOrderIds(List<String> list) throws Exception {
		return this.playerRelateConsumeMapper.selectOrderIds(list);
	}
	/**
	 * 查询消费总额
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public BigDecimal selectTotalAmountByMap(Map<String, Object> params) throws Exception {
		return this.playerRelateConsumeMapper.selectTotalAmountByMap(params);
	}
}
